package com.ruoyi.file.service.impl;

import com.ruoyi.file.service.IMinioSysFileService;
import com.ruoyi.file.utils.TencentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.file.config.MinioConfig;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * Minio 文件存储
 * 
 * @author ruoyi
 */
@Service
public class MinioSysFileServiceImpl implements IMinioSysFileService
{
    @Autowired
    private MinioConfig minioConfig;

    /**
     * 本地文件上传接口
     * 
     * @param file 上传的文件
     * @return 访问地址
     * @throws Exception
     */
    @Override
    public String uploadFile(MultipartFile file) throws Exception
    {
        TencentUtils tencentUtils = TencentUtils.getInstance(minioConfig.getUrl(),minioConfig.getAccessKey(),minioConfig.getSecretKey(),minioConfig.getBucketName());
        String url = tencentUtils.uploadFile2Cos(file);
        return url;
    }

    @Override
    public String uploadFileU(MultipartFile file, String fileName) throws Exception
    {
        TencentUtils tencentUtils = TencentUtils.getInstance(minioConfig.getUrl(),minioConfig.getAccessKey(),minioConfig.getSecretKey(),minioConfig.getBucketName());
        String url = tencentUtils.uploadFile2CosU(file, fileName);
        return url;
    }

    @Override
    public String uploadFileV(MultipartFile file, String fileNameV) throws Exception
    {
        TencentUtils tencentUtils = TencentUtils.getInstance(minioConfig.getUrl(),minioConfig.getAccessKey(),minioConfig.getSecretKey(),minioConfig.getBucketName());
        String url = tencentUtils.uploadFile2CosV(file, fileNameV, minioConfig.getPath(), minioConfig.getWatertext());
        if (null!=minioConfig.getWatertext()&&!"".equals(minioConfig.getWatertext())&&!"null".equals(minioConfig.getWatertext())) {
            String pdfPathVU = minioConfig.getPath() + "/" + fileNameV+".pdf";
            File delFilePdfU = new File(pdfPathVU);
            delFilePdfU.delete();
        }
        return url;
    }

    @Override
    public void deleteFile(String fileKey) throws Exception
    {
        TencentUtils tencentUtils = TencentUtils.getInstance(minioConfig.getUrl(),minioConfig.getAccessKey(),minioConfig.getSecretKey(),minioConfig.getBucketName());
        tencentUtils.del(fileKey);
    }

    @Override
    public void downFile(String url, String pathName) throws Exception {
        TencentUtils tencentUtils = TencentUtils.getInstance(minioConfig.getUrl(),minioConfig.getAccessKey(),minioConfig.getSecretKey(),minioConfig.getBucketName());
        tencentUtils.downfile(url, pathName);
    }
}
